home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / iwf12.zip / JUNK.C < prev    next >
C/C++ Source or Header  |  1994-02-14  |  10KB  |  325 lines

  1. /*
  2. -----------------------------------------------------------------------------
  3. int  MedianImage(IMAGE * image,RECT *r,WORD kw,WORD kh,IMAGE * new_image)
  4.  
  5. Parameters:
  6.  
  7.    *hpFB - Frame Buffer data
  8.    *r    - Rectangular region to process
  9.    kw - Number of coloums
  10.    kh - Number of rows
  11.    *hpFBdst  - Output Frame Buffer
  12.  
  13. Description:
  14.  
  15. Performs a median filtering operation on the image and places the output in
  16. a secondary image buffer.
  17. -----------------------------------------------------------------------------
  18. */
  19. IMAGE *MedianImage(IMAGE *image)
  20.    {
  21.    WORD      x,y,kw,kh;
  22.    WORD      nPixels, iMedian;
  23.    BYTE      *pixbuf;
  24.    RECT      r,r1,r2;
  25.    IMAGE    *new_image=NULL;
  26.  
  27.    kw = 3;
  28.    kh = 3;
  29.  
  30.    // Image must be at least the same size as the kernel.
  31.    if (image->hres < kw || image->vres < kh)
  32.       return(new_image);
  33.  
  34.     // Create a new image to hold the output data.
  35.     new_image = DuplicateImage(image);
  36.    CopyImageData(image,new_image);
  37.    CopyImagePal(image,new_image);
  38.  
  39.    // Determine the largest rectangle that can be processed.
  40.    SetRect(&r1,kw/2,kw/2,image->hres-kw/2,image->vres-kw/2);
  41.  
  42.    // Intersect that with the selected region.
  43.    IntersectRect(&r2,&r1,&image->rc);
  44.  
  45.    nPixels = (kw*kh);
  46.    iMedian = (kw*kh)/2;
  47.  
  48.    // Allocate memory for pixel buffer
  49.    pixbuf = (BYTE *) calloc(nPixels,(WORD)sizeof(BYTE));
  50.    if (!pixbuf)
  51.       return(new_image);
  52.  
  53.    for (y=r2.top; y<r2.bottom; y++)
  54.       for (x=r2.left; x<r2.right; x++)
  55.          {
  56.          // Copy a rectangular kernel of the image
  57.          SetRect(&r,x-kw/2,y-kh/2,x+kw/2+1,y+kh/2+1);
  58.          GetImageRect(image,&r,pixbuf);
  59.  
  60.          // Quick sort the brightness values into ascending order
  61.             qsort((void *)pixbuf,nPixels,sizeof(BYTE),ByteCompare);
  62.  
  63.          // Pick out the median or middle value as that for the pixel.
  64.          // and set the new pixel in the target image
  65.             SetImagePixel(new_image,x,y,pixbuf[iMedian]);
  66.          }
  67.  
  68.    // Free up the pixel value buffer.
  69.    free(pixbuf);
  70.  
  71.    // Return the new filtered image
  72.    return(new_image);
  73.    }
  74.  
  75. /*
  76. -----------------------------------------------------------------------------
  77. IMAGE *SobelImage(IMAGE *image,WORD Threshold,BOOL Overlay)
  78.  
  79. Parameters:
  80.  
  81.    *hpFB - Frame Buffer data
  82.    *r    - Rectangular region to process
  83.    Threshold - Threshold value
  84.    Overlay   - overlay value
  85.    *hpFBdst  - Output Frame Buffer
  86.  
  87. Description:
  88.  
  89. Performs a sobel edge detection operation on the image and places the output
  90. in a secondary image buffer.
  91. -----------------------------------------------------------------------------
  92. */
  93. IMAGE *SobelImage(IMAGE *image,WORD Threshold,BOOL Overlay)
  94.    {
  95.    BYTE       PtA, PtB, PtC, PtD, PtE, PtF, PtG, PtH, PtI;
  96.    WORD       x,y,x0,y0,x1,y1;
  97.    WORD       LineAEIAveAbove, LineAEIAveBelow, LineAEIMaxDif;
  98.    WORD       LineBEHAveAbove, LineBEHAveBelow, LineBEHMaxDif;
  99.    WORD       LineCEGAveAbove, LineCEGAveBelow, LineCEGMaxDif;
  100.    WORD       LineDEFAveAbove, LineDEFAveBelow, LineDEFMaxDif;
  101.    WORD       MaxDif;
  102.    IMAGE    *new_image=NULL;
  103.  
  104.    x0 = image->rc.left;
  105.    y0 = image->rc.top;
  106.    x1 = image->rc.right;
  107.    y1 = image->rc.bottom;
  108.  
  109.     // Create a new image to hold the output data.
  110.     new_image = DuplicateImage(image);
  111.    CopyImageData(image,new_image);
  112.    CopyImagePal(image,new_image);
  113.  
  114.    // Compensate for edge effects of 3x3 pixel neighborhood.
  115.    if (x0<1) x0 = 1;
  116.    if (y0<1) y0 = 1;
  117.    if (x1>image->hres-2) y1=image->hres-2;
  118.    if (y1>image->vres-2) y1=image->vres-2;
  119.  
  120.    for (y=y0; y<y1; y++)
  121.       for (x=x0; x<x1; x++)
  122.          {
  123.             // Get each pixel in 3x3 neighborhood.
  124.          GetImagePixel(image,x-1,y-1,&PtA);
  125.          GetImagePixel(image,x  ,y-1,&PtB);
  126.          GetImagePixel(image,x+1,y-1,&PtC);
  127.          GetImagePixel(image,x-1,y  ,&PtD);
  128.          GetImagePixel(image,x  ,y  ,&PtE);
  129.             GetImagePixel(image,x+1,y  ,&PtF);
  130.          GetImagePixel(image,x-1,y+1,&PtG);
  131.          GetImagePixel(image,x  ,y+1,&PtH);
  132.             GetImagePixel(image,x+1,y+1,&PtI);
  133.  
  134.          // Calculate average above and below the line.
  135.          // Take the absolute value of the difference.
  136.          LineAEIAveBelow = (WORD)(PtD+PtG+PtH)/3;
  137.          LineAEIAveAbove = (WORD)(PtB+PtC+PtF)/3;
  138.          LineAEIMaxDif = abs(LineAEIAveBelow-LineAEIAveAbove);
  139.  
  140.          LineBEHAveBelow = (WORD)(PtA+PtD+PtG)/3;
  141.          LineBEHAveAbove = (WORD)(PtC+PtF+PtI)/3;
  142.          LineBEHMaxDif = abs(LineBEHAveBelow-LineBEHAveAbove);
  143.  
  144.          LineCEGAveBelow = (WORD)(PtF+PtH+PtI)/3;
  145.          LineCEGAveAbove = (WORD)(PtA+PtB+PtD)/3;
  146.          LineCEGMaxDif = abs(LineCEGAveBelow-LineCEGAveAbove);
  147.  
  148.          LineDEFAveBelow = (WORD)(PtG+PtH+PtI)/3;
  149.          LineDEFAveAbove = (WORD)(PtA+PtB+PtC)/3;
  150.          LineDEFMaxDif = abs(LineDEFAveBelow-LineDEFAveAbove);
  151.  
  152.          // Find the maximum value of the absolute differences
  153.          // from the four possibilities.
  154.          MaxDif = MAX(LineAEIMaxDif,LineBEHMaxDif);
  155.          MaxDif = MAX(LineCEGMaxDif,MaxDif);
  156.          MaxDif = MAX(LineDEFMaxDif,MaxDif);
  157.  
  158.          /*
  159.          If maximum difference is above the threshold, set
  160.          the pixel of interest (center pixel) to white. If
  161.          below the threshold optionally copy the input image
  162.          to the output image. This copying is controlled by
  163.          the parameter Overlay.
  164.          */
  165.  
  166.          if (MaxDif >= Threshold)
  167.             SetImagePixel(new_image,x,y,255);
  168.          else if (Overlay)
  169.             SetImagePixel(new_image,x,y,PtE);
  170.          }
  171.  
  172.    return(new_image);
  173.    }
  174.  
  175.  
  176. /*
  177. -----------------------------------------------------------------------------
  178. int  FBArRConvolve(IMAGE * s_image,RECT *r,short *mask,WORD kw,WORD kh,WORD Scale,BOOL av_flag,IMAGE * d_image)
  179.  
  180. Parameters:
  181.  
  182.    *hpFB - Frame buffer data
  183.    *r    - rectangular region to process
  184.    *mask - Convolution kernel data
  185.    kw    - width of mask
  186.    kh    - height of mask
  187.    Scale - scaling factor
  188.    av_flag - absolute value flag
  189.    *hpFBdst - target Frame Buffer
  190.  
  191. Description:
  192.  
  193. Real Number Convolution Function. This convolution function is only used when
  194. the kernel entries are floating point numbers instead of integers. Because of 
  195. the floating point operations envolved, this function is substantially slower
  196. than the already slow integer version above.
  197. -----------------------------------------------------------------------------
  198. */
  199. int  FBArRConvolve(IMAGE * s_image,RECT *r,double *mask, WORD kw,
  200.                 WORD kh, WORD Scale,BOOL av_flag, IMAGE * d_image)
  201.    {
  202.    BYTE      dn;
  203.    WORD       x,y,w,h,x0,y0,kx, ky;
  204.    RECT      r0;
  205.    double Sum;
  206.    double *mask2;
  207.    BYTE huge *hpFBsrc;
  208.    BYTE huge *hpFBdst;
  209.  
  210.    if (r->left<0 || r->top<0 ||
  211.        r->right > s_image->hres-1 || r->bottom > s_image->vres-1)   /* Check x,y bounds     */
  212.       return(-1);
  213.  
  214.    hpFBsrc = (BYTE huge *)GlobalLock(s_image->hData);      /* Lock the IMAGE memory   */
  215.    hpFBdst = (BYTE huge *)GlobalLock(d_image->hData);      /* Lock the IMAGE memory   */
  216.  
  217.    w = r->right - r->left;
  218.    h = r->bottom - r->top;
  219.  
  220.    // Image must be at least the same size as the kernel.
  221.    if (w < kw || h < kh)
  222.       return(-1);
  223.  
  224.    /*
  225.    Clearing the output buffer to white will show the
  226.    boarder areas not touched by the convolution. It also
  227.    provides a nice white frame for the output image.
  228.    */
  229.  
  230.    SetRect(&r0,0,0,s_image->hres-1,s_image->vres-1);
  231.    SetImageRect(d_image,&r0,255);
  232.  
  233.    for (y=r->top+kh/2; y<r->bottom-kh/2; y++)
  234.       {
  235.       y0 = y - kh/2;
  236.       for (x=r->left+kw/2; x<r->right-kw/2; x++)
  237.          {
  238.          x0 = x - kw/2;
  239.          Sum = 0.0;
  240.          mask2 = mask;
  241.          for (kx=0; kx<kw; kx++)
  242.             for (ky=0; ky<kh; ky++)
  243.                {
  244.                dn = *(hpFBsrc+(DWORD)(y0+ky)*s_image->scansize+x0+kx);
  245.                Sum += dn * (*mask2++);
  246.                }
  247.  
  248.          /* If absolute value is requested */
  249.          if (av_flag)
  250.             Sum = fabs(Sum);
  251.  
  252.          /* Summation performed. Scale and range Sum */
  253.          Sum /= (double)(1<<Scale);
  254.          Sum = (Sum <   0) ?   0 : Sum;
  255.          Sum = (Sum > 255) ? 255 : Sum;
  256.  
  257.          *(hpFBdst+(DWORD)y*s_image->hres+x) = (BYTE)Sum;
  258.          }
  259.       }
  260.  
  261.    GlobalUnlock(s_image->hData);                            /* Unlock the IMAGE memory */
  262.    GlobalUnlock(d_image->hData);                            /* Unlock the IMAGE memory */
  263.  
  264.    return(0);
  265.    }
  266.  
  267. /*
  268. -------------------------------------------------------------------------------
  269. int  CopyImageData(IMAGE *s_image,IMAGE *d_image)
  270. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  271. Copies the source frame buffer data to the destination frame buffer
  272.  
  273. Parameters:
  274.  
  275.    s_image - pointer to source image
  276.    d_image - pointer to destination image
  277.  
  278. Return value:
  279.  
  280.    none
  281.  
  282. -------------------------------------------------------------------------------
  283. */
  284. void CopyImageData(IMAGE *s_image,IMAGE *d_image)
  285.    {
  286.     int  x,y,w,h;
  287.    //BYTE huge *hpDataSrc;
  288.    //BYTE huge *hpDataDst;
  289.  
  290.     //hpDataSrc = (BYTE huge *)GlobalLock(s_image->hData);    /* Lock the IMAGE memory   */
  291.    //hpDataDst = (BYTE huge *)GlobalLock(d_image->hData);    /* Lock the IMAGE memory   */
  292.  
  293.    w = (s_image->hres>d_image->hres) ? d_image->hres : s_image->hres;
  294.     h = (s_image->vres>d_image->vres) ? d_image->vres : s_image->vres;
  295.    for (y=0; y<h; ++y)
  296.       CopyImageRow(s_image,d_image,0,y,w);
  297.  
  298.       //_fmemcpy(MK_HPIMAGE(d_image,hpDataDst,0,y),MK_HPIMAGE(s_image,hpDataSrc,0,y),w);
  299.  
  300.       /*
  301.         for (x=0; x<w; ++x)
  302.          {
  303.          *(MK_HPIMAGE(d_image,hpDataDst,x,y)) =
  304.          *(MK_HPIMAGE(s_image,hpDataSrc,x,y));
  305.          //*(hpDataDst+(DWORD)(d_image->vres-1-y)*d_image->scansize+x) =
  306.          //*(hpDataSrc+(DWORD)(s_image->vres-1-y)*s_image->scansize+x);
  307.          }
  308.       */
  309.  
  310.    //GlobalUnlock(s_image->hData);                         /* Unlock the IMAGE memory */
  311.    //GlobalUnlock(d_image->hData);                         /* Unlock the IMAGE memory */
  312.     }
  313.  
  314.    /*
  315.     int  x,y,w,h;
  316.  
  317.    w = (s_image->hres>d_image->hres) ? d_image->hres : s_image->hres;
  318.     h = (s_image->vres>d_image->vres) ? d_image->vres : s_image->vres;
  319.    for (y=0; y<h; ++y)
  320.       CopyImageRow(s_image,d_image,0,y,w);
  321.    */
  322.  
  323.  
  324.  
  325.